﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.Common;
using System.Data.OleDb;
using System.Data.SqlClient;

namespace Meqs.Logic.Core
{
    /// <summary>
    /// 数据库操作抽象类
    /// dzf
    /// </summary>
    internal abstract class DBAccess : IDBAccess
    {
        protected DbConnection m_oConn = null;  //连接对象
        protected const string CON_strServer = "Data Source";   //地址字符串
        protected const string CON_strDataBase = "Initial Catalog";//数据库
        protected const string CON_strUser = "UID"; //用户
        protected const string CON_strPwd = "PWD";  //密码
        protected const string CON_strConnTimeOut = "Connect Timeout = 2";//超时时间
        private DBType m_eDBType = DBType.MsSQL;    //默认为sql
        protected object _Sign = null;

        //
        /// <summary>
        /// 抛出异常
        /// </summary>
        /// <param name="msg"></param>
        protected void ThrowException(string msg)
        {
            if (m_oConn !=null && m_oConn.State != ConnectionState.Open && m_oConn.State != ConnectionState.Connecting)
            {
                Open();
            }
            throw new DBServiceException(msg,null);
        }
        /// <summary>
        /// 抛出异常
        /// </summary>
        /// <param name="e"></param>
        protected void ThrowException(Exception e)
        {
            if (m_oConn != null && m_oConn.State != ConnectionState.Open && m_oConn.State != ConnectionState.Connecting)
            {
                Open();
            }
            //string strErr = e.Message + "\n" + e.StackTrace;
            throw new DBServiceException(e.Message, e.StackTrace, "", e);
        }
        /// <summary>
        /// 抛出异常
        /// </summary>
        /// <param name="e"></param>
        /// <param name="open"></param>
        protected void ThrowException(Exception e,bool open )
        {
            if (m_oConn != null && m_oConn.State != ConnectionState.Open && m_oConn.State != ConnectionState.Connecting && !open)
            {
                //string strErr = e.Message + "\n" + e.StackTrace;
                throw new DBServiceException(e.Message, e.StackTrace, "", e);
            }
            
        }
        /// <summary>
        /// 抛出异常
        /// </summary>
        /// <param name="e"></param>
        /// <param name="szSql"></param>
        protected void ThrowException(Exception e, string szSql)
        {
            if (m_oConn != null && m_oConn.State != ConnectionState.Open && m_oConn.State != ConnectionState.Connecting)
            {
                Open();
            }
            //string strErr = e.Message + "\n" + e.StackTrace + "\n" + szSql;
            throw new DBServiceException(e.Message, e.StackTrace, szSql, e);
        }
        //
        /// <summary>
        /// 构造
        /// </summary>
        /// <param name="type"></param>
        protected DBAccess(DBType type)
        {
            this.m_eDBType = type;
        }
        //
        /// <summary>
        /// 获取类型
        /// </summary>
        public DBType DBType
        {
            get { return this.m_eDBType; }
        }
        //
        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="strServer"></param>
        /// <param name="port"></param>
        /// <param name="strDataBase"></param>
        /// <param name="strUser"></param>
        /// <param name="strPwd"></param>
        public void Init(string strServer, string port, string strDataBase, string strUser, string strPwd)
        {
            this.InitConn(strServer, port, strDataBase, strUser, strPwd);
        }
        //
        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="strServer"></param>
        public void Init(string strServer)
        {
            this.InitConn(strServer);
        }
        //
        /// <summary>
        /// 打开
        /// </summary>
        virtual public void Open()
        {
            if (this.m_oConn != null)
            {
                try
                {
                    this.m_oConn.Open();
                }
                catch (Exception ex)
                {
                    ThrowException(ex,false);
                }

            }
            else
            {
                ThrowException("未进行初始化");
            }

        }
        //
        /// <summary>
        /// 执行关闭
        /// </summary>
        public abstract void Close();
        //{
        //    if (this.m_oConn != null && this.m_oConn.State == System.Data.ConnectionState.Open)
        //    {
        //        this.m_oConn.Close();
        //        DbConnection.ClearAllPools();

        //    }
        //}
        //
        /// <summary>
        /// 增 删 改
        /// </summary>
        /// <param name="strCmd"></param>
        /// <returns></returns>
        public abstract int RunNoQuery(string strCmd);
        //{
        //    int iRet = 0;
        //    try
        //    {
        //        DbCommand oCmd = this.GetCmd(strCmd);
        //        //OleDbParameter pt=new OleDbParameter();
        //        //pt.OleDbType
        //        //oCmd.Parameters.Add
        //        if (oCmd != null)
        //        {
        //            iRet = oCmd.ExecuteNonQuery();
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        ThrowException(ex, strCmd);
        //        //throw (new Exception(ex.Message));
        //    }
        //    return iRet;
        //}

        //]
        /// <summary>
        /// 获取某字段的最大值
        /// </summary>
        /// <param name="strTable"></param>
        /// <param name="strField"></param>
        /// <returns></returns>
        public abstract int GetFiledMax(string strTable, string strField);
        //{
        //    int iRet = -1;
        //    DataTable dt;
        //    try
        //    {
        //        dt = this.RunQuery("Select Max(" + strField + ") From " + strTable);
        //        if (dt != null && dt.Rows.Count == 1)
        //        {
        //            iRet = dt.Rows[0][0] is DBNull ? 0 : Convert.ToInt32(dt.Rows[0][0]);
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        ThrowException(ex);
        //    }

        //    return iRet;
        //}
        //
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="strCmd"></param>
        /// <returns></returns>
        public abstract DataTable RunQuery(string strCmd);
        //{
        //    DataTable dt = new DataTable();
        //    DbDataAdapter adp = this.DbAdp;
        //    try
        //    {
        //        adp.SelectCommand = this.GetCmd(strCmd);
        //        adp.Fill(dt);
        //    }
        //    catch (Exception ex)
        //    {
        //        ThrowException(ex, strCmd);
        //    }

        //    return dt;
        //}
        /// <summary>
        /// 提交事务
        /// </summary>
        /// <param name="sign"></param>
        public abstract void CommitTransaction(object sign);
        //{
        //    if (sign == null)
        //        return;
        //    DbTransaction dbtrans = (DbTransaction)sign;
        //    try
        //    {
        //        dbtrans.Commit();

        //    }
        //    catch (Exception ex)
        //    {
        //        ThrowException(ex);
        //    }
        //    finally
        //    {
        //        _Sign = null;
        //        dbtrans = null;
        //    }
        //}
        /// <summary>
        /// 事务回滚
        /// </summary>
        /// <param name="sign"></param>
        public abstract void RollBackTransaction(object sign);
        //{
        //    if (sign == null)
        //        return;
        //    DbTransaction dbtrans = (DbTransaction)sign;
        //    try
        //    {
        //        dbtrans.Rollback();

        //    }
        //    catch (Exception ex)
        //    {
        //        ThrowException(ex);
        //    }
        //    finally
        //    {
        //        _Sign = null;
        //        dbtrans = null;
        //    }

        //}
        /// <summary>
        /// 开始事务
        /// </summary>
        /// <returns></returns>
        public abstract object StartTransaction();
        //public abstract void CommitTransaction(object sign);
        //public abstract void RollBackTransaction(object sign);
        /// <summary>
        /// 执行查询
        /// </summary>
        /// <param name="strCmd"></param>
        /// <param name="name"></param>
        /// <param name="paramer"></param>
        /// <returns></returns>
        public abstract DataTable RunQuery(string strCmd, string[] name, object[] paramer);
        /// <summary>
        /// 执行增删改.返回操作结果
        /// </summary>
        /// <param name="strCmd"></param>
        /// <param name="name"></param>
        /// <param name="paramer"></param>
        /// <returns></returns>
        public abstract int RunNoQuery(string strCmd, string[] name, object[] paramer);
        public abstract int RunNoQuery(string strCmd, string name, byte[] image, long len);
        public abstract int RunNoQuery(string insertStrCmd, string[] name, object[] paramer, ref int newNum);
        //
        /// <summary>
        /// 获取表信息
        /// </summary>
        public abstract DataTable Tables { get; }
        //
        /// <summary>
        /// 获取数据库中所有列信息
        /// </summary>
        /// <returns></returns>
        public abstract DataTable GetColumns();
        //
        /// <summary>
        /// 获取某张表的列信息
        /// </summary>
        /// <param name="strTable"></param>
        /// <returns></returns>
        public abstract DataTable GetColumns(string strTable);
        //
        /// <summary>
        /// 初始化连接
        /// </summary>
        /// <param name="strServer"></param>
        /// <param name="port"></param>
        /// <param name="strDataBase"></param>
        /// <param name="strUser"></param>
        /// <param name="strPwd"></param>
        public abstract void InitConn(string strServer, string port, string strDataBase, string strUser, string strPwd);
        //
        /// <summary>
        /// 初始化连接
        /// </summary>
        /// <param name="strServer"></param>
        public abstract void InitConn(string strServer);
        //
        /// <summary>
        /// 获得CMD
        /// </summary>
        /// <param name="strCmd"></param>
        /// <returns></returns>
        public abstract DbCommand GetCmd(string strCmd);
        //
        /// <summary>
        /// 获得数据适配器
        /// </summary>
        public abstract DbDataAdapter DbAdp { get;}
        /// <summary>
        /// 执行非查询sql
        /// </summary>
        /// <param name="procName"></param>
        /// <param name="ParaValue"></param>
        /// <param name="ParaName"></param>
        /// <param name="ParaType"></param>
        /// <param name="ParaDire"></param>
        public abstract void ExecProcNonQuery(string procName, object[] ParaValue, string[] ParaName, DBParamerType[] ParaType, DBParameterDirection[] ParaDire);
        /// <summary>
        /// 执行查询sql
        /// </summary>
        /// <param name="procName"></param>
        /// <param name="ParaValue"></param>
        /// <param name="ParaName"></param>
        /// <param name="ParaType"></param>
        /// <param name="ParaDire"></param>
        /// <returns></returns>
        public abstract DataTable ExecProcQuery(string procName, object[] ParaValue, string[] ParaName, DBParamerType[] ParaType, DBParameterDirection[] ParaDire);
        /// <summary>
        /// 更新表
        /// </summary>
        /// <param name="strTblName"></param>
        /// <param name="dt"></param>
        /// <param name="pk"></param>
        /// <returns></returns>
        public abstract int UpdateDataTable(string strTblName, DataTable dt, string pk);
        //{
        //    int re = 0;
        //    if (dt.Rows.Count > 0)
        //    {
        //        foreach (DataRow row in dt.Rows)
        //        {
        //            string exsql = "update " + strTblName + " set ";
        //            string sed = "where ";
        //            foreach (DataColumn column in dt.Columns)
        //            {
        //                if (column.Caption.ToLower() != pk.ToLower())
        //                {
        //                    exsql += " " + column.Caption + "='" + @row[column] + "' ,";
        //                }
        //                else
        //                {
        //                    sed += column.Caption + "='" + @row[column] + "'";
        //                }
        //            }
        //            exsql = exsql.Substring(0, exsql.Length - 1);
        //            exsql += sed;

        //            re += RunNoQuery(exsql);
        //        }
        //    }
        //    return re;
        //}
    }
}